package fragment;

import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;

import com.example.admintor.humaninteraction.Data;
import com.example.admintor.humaninteraction.LoginActivity;
import com.example.admintor.humaninteraction.R;

import java.lang.reflect.Field;

/**
 * Created by Admintor on 2017/11/9.
 */

public class SixFragment extends Fragment implements View.OnClickListener {
    ImageView c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11, c12, c13, c14, c15, c16;
    ImageView deng;
    Button open;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_six, container, false);
        FindView(view);
        Onclick();
        return view;
    }

    @Override
    public void onDetach() {
        super.onDetach();
        try {
            Field childFragmentManager = Fragment.class.getDeclaredField("mChildFragmentManager");
            childFragmentManager.setAccessible(true);
            childFragmentManager.set(this, null);


        } catch (NoSuchFieldException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    private void FindView(View view) {
        c1 = (ImageView) view.findViewById(R.id.c1);
        c2 = (ImageView) view.findViewById(R.id.c2);
        c3 = (ImageView) view.findViewById(R.id.c3);
        c4 = (ImageView) view.findViewById(R.id.c4);
        c5 = (ImageView) view.findViewById(R.id.c5);
        c6 = (ImageView) view.findViewById(R.id.c6);
        c7 = (ImageView) view.findViewById(R.id.c7);
        c8 = (ImageView) view.findViewById(R.id.c8);
        c9 = (ImageView) view.findViewById(R.id.c9);
        c10 = (ImageView) view.findViewById(R.id.c10);
        c11 = (ImageView) view.findViewById(R.id.c11);
        c12 = (ImageView) view.findViewById(R.id.c12);
        c13 = (ImageView) view.findViewById(R.id.c13);
        c14 = (ImageView) view.findViewById(R.id.c14);
        c15 = (ImageView) view.findViewById(R.id.c15);
        c16 = (ImageView) view.findViewById(R.id.c16);
        deng = (ImageView) view.findViewById(R.id.deng);
        open = (Button) view.findViewById(R.id.open);
    }

    private void Onclick() {
        c1.setOnClickListener(this);
        c2.setOnClickListener(this);
        c3.setOnClickListener(this);
        c4.setOnClickListener(this);
        c5.setOnClickListener(this);
        c6.setOnClickListener(this);
        c7.setOnClickListener(this);
        c8.setOnClickListener(this);
        c9.setOnClickListener(this);
        c10.setOnClickListener(this);
        c11.setOnClickListener(this);
        c12.setOnClickListener(this);
        c13.setOnClickListener(this);
        c14.setOnClickListener(this);
        c15.setOnClickListener(this);
        c16.setOnClickListener(this);
        open.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.c1:
                LoginActivity.landing.setKeyByUiid(Data.RGB, 1);
                ClearView();
                c1.setBackgroundResource(R.mipmap.c1o);
                deng.setBackgroundResource(R.mipmap.c1_d);
                break;
            case R.id.c2:
                LoginActivity.landing.setKeyByUiid(Data.RGB, 2);
                ClearView();
                c2.setBackgroundResource(R.mipmap.c2o);
                deng.setBackgroundResource(R.mipmap.c2_d);
                break;
            case R.id.c3:
                LoginActivity.landing.setKeyByUiid(Data.RGB, 3);
                ClearView();
                c3.setBackgroundResource(R.mipmap.c3o);
                deng.setBackgroundResource(R.mipmap.c3_d);
                break;
            case R.id.c4:
                LoginActivity.landing.setKeyByUiid(Data.RGB, 4);
                ClearView();
                c4.setBackgroundResource(R.mipmap.c4o);
                deng.setBackgroundResource(R.mipmap.c4_d);
                break;
            case R.id.c5:
                LoginActivity.landing.setKeyByUiid(Data.RGB, 5);
                ClearView();
                c5.setBackgroundResource(R.mipmap.c5o);
                deng.setBackgroundResource(R.mipmap.c5_d);
                break;
            case R.id.c6:
                LoginActivity.landing.setKeyByUiid(Data.RGB, 6);
                ClearView();
                c6.setBackgroundResource(R.mipmap.c6o);
                deng.setBackgroundResource(R.mipmap.c6_d);
                break;
            case R.id.c7:
                LoginActivity.landing.setKeyByUiid(Data.RGB, 7);
                ClearView();
                c7.setBackgroundResource(R.mipmap.c7o);
                deng.setBackgroundResource(R.mipmap.c7_d);
                break;
            case R.id.c8:
                LoginActivity.landing.setKeyByUiid(Data.RGB, 8);
                ClearView();
                c8.setBackgroundResource(R.mipmap.c8o);
                deng.setBackgroundResource(R.mipmap.c8_d);
                break;
            case R.id.c9:
                LoginActivity.landing.setKeyByUiid(Data.RGB, 9);
                ClearView();
                c9.setBackgroundResource(R.mipmap.c9o);
                deng.setBackgroundResource(R.mipmap.c9_d);
                break;
            case R.id.c10:
                LoginActivity.landing.setKeyByUiid(Data.RGB, 10);
                ClearView();
                c10.setBackgroundResource(R.mipmap.c10o);
                deng.setBackgroundResource(R.mipmap.c10_d);
                break;
            case R.id.c11:
                LoginActivity.landing.setKeyByUiid(Data.RGB, 11);
                ClearView();
                c11.setBackgroundResource(R.mipmap.c11o);
                deng.setBackgroundResource(R.mipmap.c11_d);
                break;
            case R.id.c12:
                LoginActivity.landing.setKeyByUiid(Data.RGB, 12);
                ClearView();
                c12.setBackgroundResource(R.mipmap.c12o);
                deng.setBackgroundResource(R.mipmap.c12_d);
                break;
            case R.id.c13:
                LoginActivity.landing.setKeyByUiid(Data.RGB, 13);
                ClearView();
                c13.setBackgroundResource(R.mipmap.c13o);
                deng.setBackgroundResource(R.mipmap.c13_d);
                break;
            case R.id.c14:
                LoginActivity.landing.setKeyByUiid(Data.RGB, 14);
                ClearView();
                c14.setBackgroundResource(R.mipmap.c14o);
                deng.setBackgroundResource(R.mipmap.c14_d);
                break;
            case R.id.c15:
                LoginActivity.landing.setKeyByUiid(Data.RGB, 15);
                ClearView();
                c15.setBackgroundResource(R.mipmap.c15o);
                deng.setBackgroundResource(R.mipmap.c15_d);
                break;
            case R.id.c16:
                LoginActivity.landing.setKeyByUiid(Data.RGB, 16);
                ClearView();
                c16.setBackgroundResource(R.mipmap.c16o);
                deng.setBackgroundResource(R.mipmap.c16_d);
                break;
            case R.id.open:
                if (state == false) {
                    LoginActivity.landing.setKeyByUiid(Data.RGB, 17);
                    state = true;
                } else {
                    LoginActivity.landing.setKeyByUiid(Data.RGB, 18);
                    state = false;
                }
                break;
        }
    }

    private boolean state = false;

    private void ClearView() {
        c1.setBackgroundResource(R.mipmap.c1);
        c2.setBackgroundResource(R.mipmap.c2);
        c3.setBackgroundResource(R.mipmap.c3);
        c4.setBackgroundResource(R.mipmap.c4);
        c5.setBackgroundResource(R.mipmap.c5);
        c6.setBackgroundResource(R.mipmap.c6);
        c7.setBackgroundResource(R.mipmap.c7);
        c8.setBackgroundResource(R.mipmap.c8);
        c9.setBackgroundResource(R.mipmap.c9);
        c10.setBackgroundResource(R.mipmap.c10);
        c11.setBackgroundResource(R.mipmap.c11);
        c12.setBackgroundResource(R.mipmap.c12);
        c13.setBackgroundResource(R.mipmap.c13);
        c14.setBackgroundResource(R.mipmap.c14);
        c15.setBackgroundResource(R.mipmap.c15);
        c16.setBackgroundResource(R.mipmap.c16);
    }
}
